{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Zero Matrix LCCI"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Medium"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #LCCI"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #hash-table #matrix"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #哈希表 #矩阵"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: setZeroes"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #零矩阵"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>编写一种算法，若M × N矩阵中某个元素为0，则将其所在的行与列清零。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>\n",
    "[\n",
    "  [1,1,1],\n",
    "  [1,0,1],\n",
    "  [1,1,1]\n",
    "]\n",
    "<strong>输出：</strong>\n",
    "[\n",
    "  [1,0,1],\n",
    "  [0,0,0],\n",
    "  [1,0,1]\n",
    "]\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>\n",
    "[\n",
    "  [0,1,2,0],\n",
    "  [3,4,5,2],\n",
    "  [1,3,1,5]\n",
    "]\n",
    "<strong>输出：</strong>\n",
    "[\n",
    "  [0,0,0,0],\n",
    "  [0,4,5,0],\n",
    "  [0,3,1,0]\n",
    "]\n",
    "</pre>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [zero-matrix-lcci](https://leetcode.cn/problems/zero-matrix-lcci/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [zero-matrix-lcci](https://leetcode.cn/problems/zero-matrix-lcci/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[[1,1,1],[1,0,1],[1,1,1]]', '[[0,1,2,0],[3,4,5,2],[1,3,1,5]]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def setZeroes(self, matrix: List[List[int]]) -> None:\n",
    "        m,n=len(matrix),len(matrix[0])\n",
    "        x,y=m*[False],n*[False]\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if matrix[i][j]==0:\n",
    "                    x[i],y[j]=True,True\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if x[i] or y[j]: matrix[i][j]=0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def setZeroes(self, matrix: List[List[int]]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify matrix in-place instead.\n",
    "        \"\"\"\n",
    "        r, c = len(matrix), len(matrix[0])\n",
    "        tmp_pointer = []\n",
    "        def changeZero(x, y):\n",
    "            for i in range(r):\n",
    "                if matrix[i][y] != 0:\n",
    "                    matrix[i][y] = 0\n",
    "                    tmp_pointer.append((i, y))\n",
    "            for i in range(c):\n",
    "                if matrix[x][i] != 0:\n",
    "                    matrix[x][i] = 0\n",
    "                    tmp_pointer.append((x, i))\n",
    "        \n",
    "        for i in range(r):\n",
    "            for j in range(c):\n",
    "                if matrix[i][j] == 0 and (i, j) not in tmp_pointer:\n",
    "                    tmp_pointer.append((i, j))\n",
    "                    changeZero(i, j)\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def setZeroes(self, matrix: List[List[int]]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify matrix in-place instead.\n",
    "        \"\"\"\n",
    "        for i in range(len(matrix)):\n",
    "            for j in range(len(matrix[0])):\n",
    "                if int(matrix[i][j]) == 0:\n",
    "                    for l in range(len(matrix)):\n",
    "                        matrix[l][j] = str(matrix[l][j])\n",
    "                    for l in range(len(matrix[0])):\n",
    "                        matrix[i][l] = str(matrix[i][l])\n",
    "        for i in range(len(matrix)):\n",
    "            for j in range(len(matrix[0])):\n",
    "                if int(int(matrix[i][j])) != matrix[i][j]:\n",
    "                    matrix[i][j] = 0\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def setZeroes(self, matrix: List[List[int]]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify matrix in-place instead.\n",
    "        \"\"\"\n",
    "        rows = set()\n",
    "        cols = set()\n",
    "        m = len(matrix)\n",
    "        n = len(matrix[0])\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if matrix[i][j] == 0:\n",
    "                    rows.add(i)\n",
    "                    cols.add(j)\n",
    "        for i in rows:\n",
    "            matrix[i] = [0]*n\n",
    "        for i in cols:\n",
    "            for j in range(m):\n",
    "                matrix[j][i] = 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def setZeroes(self, matrix: List[List[int]]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify matrix in-place instead.\n",
    "        \"\"\"\n",
    "        M=len(matrix)\n",
    "        N=len(matrix[0])\n",
    "\n",
    "        r_zero = set()\n",
    "        c_zero = set()\n",
    "        for i in range(M):\n",
    "            for j in range(N):\n",
    "                if matrix[i][j] == 0:\n",
    "                    r_zero.add(i)\n",
    "                    c_zero.add(j)\n",
    "\n",
    "        for i in r_zero:\n",
    "            for j in range(N):\n",
    "                matrix[i][j] = 0\n",
    "\n",
    "        for j in c_zero:\n",
    "            for i in range(M):\n",
    "                matrix[i][j] = 0\n",
    "                \n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def setZeroes(self, matrix: List[List[int]]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify matrix in-place instead.\n",
    "        \"\"\"\n",
    "        row=set()\n",
    "        column=set()\n",
    "        for i in range(len(matrix)):\n",
    "            for j in range(len(matrix[0])):\n",
    "                if matrix[i][j]==0:\n",
    "                    row.add(i)\n",
    "                    column.add(j)\n",
    "        for i in row:\n",
    "            for j in range(len(matrix[0])):\n",
    "                matrix[i][j]=0\n",
    "        for j in column:\n",
    "            for i in range(len(matrix)):\n",
    "                matrix[i][j]=0\n",
    "        return\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def setZeroes(self, matrix: List[List[int]]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify matrix in-place instead.\n",
    "        \"\"\"\n",
    "        visited = []\n",
    "        def modify_zero(i, j):\n",
    "            if matrix[i][j] != 0:\n",
    "                return\n",
    "            if matrix[i][j] == 0 and (i, j) in visited:\n",
    "                return\n",
    "            for a in range(len(matrix[0])):\n",
    "                if matrix[i][a] != 0:\n",
    "                    matrix[i][a] = 0\n",
    "                    visited.append((i, a))\n",
    "            for b in range(len(matrix)):\n",
    "                if matrix[b][j] != 0:\n",
    "                    matrix[b][j] = 0\n",
    "                    visited.append((b, j))\n",
    "        \n",
    "        for i in range(len(matrix)):\n",
    "            for j in range(len(matrix[0])):\n",
    "                modify_zero(i, j)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def setZeroes(self, matrix: List[List[int]]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify matrix in-place instead.\n",
    "        \"\"\"\n",
    "        rows = set()\n",
    "        columns = set()\n",
    "        M = len(matrix)\n",
    "        N = len(matrix[0])\n",
    "        # 寻找存在0的行与列\n",
    "        for i in range(M):\n",
    "            for j in range(N):\n",
    "                if matrix[i][j] == 0:\n",
    "                    rows.add(i)\n",
    "                    columns.add(j)\n",
    "        for i in rows:\n",
    "            for j in range(N):\n",
    "                matrix[i][j]=0\n",
    "        for i in columns:\n",
    "            for j in range(M):\n",
    "                matrix[j][i]=0\n",
    "\n",
    "    '''\n",
    "        M = len(matrix)\n",
    "        N = len(matrix[0])\n",
    "        points = self.find_zero(matrix, M, N)\n",
    "        for point in points:\n",
    "            for i in range(N):\n",
    "                matrix[point[0]][i] =0\n",
    "            for i in range(M):\n",
    "                matrix[i][point[1]] =0\n",
    "\n",
    "    def find_zero(self, matrix, M, N):\n",
    "        point = []\n",
    "        for i in range(M):\n",
    "            for j in range(N):\n",
    "                if matrix[i][j] == 0:\n",
    "                    point.append((i,j))\n",
    "        return  point   \n",
    "    #'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def setZeroes(self, matrix: List[List[int]]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify matrix in-place instead.\n",
    "        \"\"\"\n",
    "        m,n=len(matrix),len(matrix[0])\n",
    "\n",
    "        row0 = set()\n",
    "        col0 = set()\n",
    "        # flag_row0 = any(matrix[0][i] == 0 for i in range(n))\n",
    "        # flag_col0 = any(matrix[i][0] == 0 for i in range(m))\n",
    "\n",
    "        # for i in range(1,m):\n",
    "        #     for j in range(1,n):\n",
    "        #         if matrix[i][j] == 0:\n",
    "        #             matrix[i][0] = matrix[0][j] = 0\n",
    "        \n",
    "        # for i in range(1,m):\n",
    "        #     for j in range(1,n):\n",
    "        #         if matrix[i][0] == 0 or matrix[0][j] == 0:\n",
    "        #             matrix[i][j] = 0\n",
    "        \n",
    "        # if flag_col0:\n",
    "        #     for i in range(m):\n",
    "        #         matrix[i][0] = 0\n",
    "        \n",
    "        # if flag_row0:\n",
    "        #     for j in range(n):\n",
    "        #         matrix[0][j] = 0\n",
    "\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if matrix[i][j] == 0:\n",
    "                    row0.add(i)\n",
    "                    col0.add(j)\n",
    "        \n",
    "        for row in row0:\n",
    "            matrix[row] = [0]*n\n",
    "\n",
    "        for col in col0:\n",
    "            for i in range(m):\n",
    "                matrix[i][col] = 0        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def setZeroes(self, matrix: List[List[int]]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify matrix in-place instead.\n",
    "        \"\"\"\n",
    "        rec_row = [False] * len(matrix)\n",
    "        rec_col = [False] * len(matrix[0])\n",
    "\n",
    "        for i in range(len(matrix)):\n",
    "            for j in range(len(matrix[0])):\n",
    "                if matrix[i][j] == 0:\n",
    "                    rec_row[i] = True\n",
    "                    rec_col[j] = True\n",
    "\n",
    "        for i in range(len(matrix)):\n",
    "            for j in range(len(matrix[0])):\n",
    "                if rec_row[i] or rec_col[j]:\n",
    "                    matrix[i][j] = 0\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def setZeroes(self, matrix: List[List[int]]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify matrix in-place instead.\n",
    "        \"\"\"\n",
    "        m  = len(matrix)\n",
    "        n = len(matrix[0])\n",
    "        row = [False] * m\n",
    "        col = [False] * n\n",
    "        \n",
    "        for i in range(len(matrix)):\n",
    "            for j in range(len(matrix[0])):\n",
    "                if matrix[i][j] == 0:\n",
    "                    row[i] = True\n",
    "                    col[j] = True\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if row[i] or col[j]:\n",
    "                    matrix[i][j]=0\n",
    "        \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def setZeroes(self, matrix: List[List[int]]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify matrix in-place instead.\n",
    "        \"\"\"\n",
    "        rows = set()  \n",
    "        cols = set()  \n",
    "        m = len(matrix)  \n",
    "        n = len(matrix[0])  \n",
    "        for i in range(m):  \n",
    "            for j in range(n):  \n",
    "                if matrix[i][j] == 0:  \n",
    "                    rows.add(i)  \n",
    "                    cols.add(j)  \n",
    "        for i in range(m):  \n",
    "            for j in range(n):  \n",
    "                if i in rows or j in cols:  \n",
    "                    matrix[i][j] = 0\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def setZeroes(self, matrix: List[List[int]]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify matrix in-place instead.\n",
    "        \"\"\"\n",
    "\n",
    "        m = len(matrix)\n",
    "        n = len(matrix[0])\n",
    "\n",
    "        row = [False] * m\n",
    "        col = [False] * n\n",
    "\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if matrix[i][j] == 0:\n",
    "                    row[i] = True\n",
    "                    col[j] = True\n",
    "\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if row[i] or col[j]:\n",
    "                    matrix[i][j] = 0\n",
    "\n",
    "        return matrix \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def setZeroes(self, matrix: List[List[int]]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify matrix in-place instead.\n",
    "        \"\"\"\n",
    "\n",
    "        rows = set()\n",
    "        columns = set()\n",
    "\n",
    "        for i in range(len(matrix)):\n",
    "            for j in range(len(matrix[0])):\n",
    "                if matrix[i][j] == 0:\n",
    "                    rows.add(i)\n",
    "                    columns.add(j)\n",
    "\n",
    "        for r in rows:\n",
    "            for j in range(len(matrix[0])):\n",
    "                matrix[r][j]=0\n",
    "        for c in columns:\n",
    "            for i in range(len(matrix)):\n",
    "                matrix[i][c]=0\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def setZeroes(self, matrix: List[List[int]]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify matrix in-place instead.\n",
    "        \"\"\"\n",
    "        row,col = len(matrix),len(matrix[0])\n",
    "        zero_row ,zero_col = set(),set()\n",
    "\n",
    "        for i in range(row):\n",
    "            for j in range(col):\n",
    "                if matrix[i][j] == 0:\n",
    "                    zero_row.add(i)\n",
    "                    zero_col.add(j)\n",
    "        \n",
    "        for i in zero_row:\n",
    "            matrix[i] = [0] * col\n",
    "        \n",
    "        for j in zero_col:\n",
    "            for i in range(row):\n",
    "                matrix[i][j] = 0\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def setZeroes(self, matrix: List[List[int]]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify matrix in-place instead.\n",
    "        \"\"\"\n",
    "        row = len(matrix)\n",
    "        col = len(matrix[0])\n",
    "        pos_row = set()\n",
    "        pos_col = set()\n",
    "\n",
    "        for i in range(row):\n",
    "            for j in range(col):\n",
    "                if not matrix[i][j]:\n",
    "                    pos_row.add(i)\n",
    "                    pos_col.add(j)\n",
    "        for i in range(row):\n",
    "            for j in range(col):\n",
    "                if i in pos_row or j in pos_col:\n",
    "                    matrix[i][j] = 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def setZeroes(self, matrix: List[List[int]]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify matrix in-place instead.\n",
    "        \"\"\"\n",
    "        row = []\n",
    "        column = []\n",
    "        n_r, n_c = len(matrix), len(matrix[0])\n",
    "        for _ in range(n_r):\n",
    "            for __ in range(n_c):\n",
    "                if matrix[_][__] == 0:\n",
    "                    if _ not in row:\n",
    "                        row.append(_)\n",
    "                    if __ not in column:\n",
    "                        column.append(__)\n",
    "        for i in range(len(row)):\n",
    "            for _ in range(n_c):\n",
    "                matrix[row[i]][_] = 0\n",
    "        for j in range(len(column)):\n",
    "            for _ in range(n_r):\n",
    "                matrix[_][column[j]] = 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def setZeroes(self, matrix: List[List[int]]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify matrix in-place instead.\n",
    "        \"\"\"\n",
    "        row = []\n",
    "        col= []\n",
    "        m=len(matrix)\n",
    "        n=len(matrix[0])\n",
    "        for r in range(m):\n",
    "            for c in range(n):\n",
    "                if matrix[r][c] ==0:\n",
    "                    row.append(r)\n",
    "                    col.append(c)\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if i in row or j in col:\n",
    "                    \n",
    "                    matrix[i][j]=0\n",
    "                \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def setZeroes(self, matrix: List[List[int]]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify matrix in-place instead.\n",
    "        \"\"\"\n",
    "        if not matrix:\n",
    "            return []\n",
    "        rows,cols = len(matrix),len(matrix[0])\n",
    "        rows_zero,cols_zero = set(),set()\n",
    "        for i in range(rows):\n",
    "            for j in range(cols):\n",
    "                if matrix[i][j] == 0:\n",
    "                    rows_zero.add(i)\n",
    "                    cols_zero.add(j)\n",
    "        for j in cols_zero:\n",
    "            for i in range(rows):\n",
    "                matrix[i][j]=0\n",
    "        for i in rows_zero:\n",
    "            for j in range(cols):\n",
    "                matrix[i][j]=0\n",
    "\n",
    "                    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def setZeroes(self, matrix: List[List[int]]) -> None:\n",
    "        result = []\n",
    "        rows, cols = len(matrix), len(matrix[0])\n",
    "        for i in range(rows):\n",
    "            for j in range(cols):\n",
    "                if matrix[i][j] == 0:\n",
    "                    result.append((i, j))\n",
    "        for r, c in result:\n",
    "            for i in range(rows):\n",
    "                matrix[i][c] = 0\n",
    "            for i in range(cols):\n",
    "                matrix[r][i] = 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def setZeroes(self, matrix: List[List[int]]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify matrix in-place instead.\n",
    "        \"\"\"\n",
    "        m,n = len(matrix),len(matrix[0])\n",
    "        zero_rows,zero_cols = set(),set()\n",
    "\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if matrix[i][j] == 0:\n",
    "                    zero_rows.add(i)\n",
    "                    zero_cols.add(j)\n",
    "        \n",
    "        for i in zero_rows:\n",
    "            for j in range(n):\n",
    "                matrix[i][j] = 0\n",
    "        \n",
    "        for j in zero_cols:\n",
    "            for i in range(m):\n",
    "                matrix[i][j] = 0\n",
    "        \n",
    "        return matrix"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def setZeroes(self, matrix: list[list[int]]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify matrix in-place instead.\n",
    "        \"\"\"\n",
    "        hang = len(matrix)\n",
    "        lie = len(matrix[0])\n",
    "        zero_hang=set()\n",
    "        zero_lie=set()\n",
    "        for i in range(0, hang):\n",
    "            for k in range(0, lie):\n",
    "                if matrix[i][k] == 0:\n",
    "                    zero_hang.add(i)\n",
    "                    zero_lie.add(k)\n",
    "        for i in zero_hang:\n",
    "            matrix[i]=[0]*lie\n",
    "        for k in zero_lie:\n",
    "            for j in range(0,hang):\n",
    "                matrix[j][k]=0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def setZeroes(self, matrix: List[List[int]]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify matrix in-place instead.\n",
    "        \"\"\"\n",
    "        m = len(matrix)  # 获取矩阵的行数\n",
    "        n = len(matrix[0])  # 获取矩阵的列数\n",
    "\n",
    "        zero_rows = set()  # 用于存储出现 0 的行的索引\n",
    "        zero_cols = set()  # 用于存储出现 0 的列的索引\n",
    "\n",
    "        # 遍历整个矩阵，找到值为 0 的元素的位置\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if matrix[i][j] == 0:\n",
    "                    zero_rows.add(i)  # 将行索引添加到集合 zero_rows 中\n",
    "                    zero_cols.add(j)  # 将列索引添加到集合 zero_cols 中\n",
    "        # 将集合 zero_cols 中的列置为 0\n",
    "        for col in zero_cols:\n",
    "            for i in range(m):\n",
    "                matrix[i][col] = 0\n",
    "\n",
    "        # 将集合 zero_rows 中的行置为 0\n",
    "        for row in zero_rows:\n",
    "            matrix[row] = [0] * n\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def setZeroes(self, matrix: List[List[int]]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify matrix in-place instead.\n",
    "        \"\"\"\n",
    "        row_zero = set()\n",
    "        col_zero = set()\n",
    "        for i in range(len(matrix)):\n",
    "            for j in range(len(matrix[i])):\n",
    "                if matrix[i][j] == 0:\n",
    "                    row_zero.add(i)\n",
    "                    col_zero.add(j)\n",
    "        \n",
    "        for i in range(len(matrix)):\n",
    "            for j in range(len(matrix[i])):\n",
    "                if i in row_zero or j in col_zero:\n",
    "                    matrix[i][j] = 0\n",
    "        return matrix"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def setZeroes(self, matrix: List[List[int]]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify matrix in-place instead.\n",
    "        \"\"\"\n",
    "        m, n = len(matrix), len(matrix[0])\n",
    "        mat_col0 = False\n",
    "        for i in range(m):\n",
    "            if not matrix[i][0]:\n",
    "                mat_col0 = True\n",
    "            for j in range(1, n):\n",
    "                if matrix[i][j] == 0:\n",
    "                    matrix[i][0] = matrix[0][j] = 0\n",
    "        for i in range(m - 1, -1, -1):\n",
    "            for j in range(1, n):\n",
    "                if not matrix[i][0] or not matrix[0][j]:\n",
    "                    matrix[i][j] = 0\n",
    "            if mat_col0:\n",
    "                matrix[i][0] = 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def setZeroes(self, matrix: List[List[int]]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify matrix in-place instead.\n",
    "        \"\"\"\n",
    "        zr = []\n",
    "        zc = []\n",
    "        m = len(matrix)\n",
    "        n = len(matrix[0])\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if matrix[i][j]==0:\n",
    "                    if i not in zr:\n",
    "                        zr.append(i)\n",
    "                    if j not in zc:\n",
    "                        zc.append(j)\n",
    "        for i in zr:\n",
    "            for j in range(n):\n",
    "                matrix[i][j]=0\n",
    "        for i in range(m):\n",
    "            for j in zc:\n",
    "                matrix[i][j]=0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def setZeroes(self, matrix: List[List[int]]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify matrix in-place instead.\n",
    "        \"\"\"\n",
    "        # rec_row = [False] * len(matrix)\n",
    "        # rec_col = [False] * len(matrix[0])\n",
    "        fst_row = False\n",
    "        fst_col = False\n",
    "\n",
    "        for j in range(len(matrix[0])):\n",
    "            if matrix[0][j] == 0:\n",
    "                fst_row = True\n",
    "        for i in range(len(matrix)):\n",
    "            if matrix[i][0] == 0:\n",
    "                fst_col = True\n",
    "\n",
    "        for i in range(1, len(matrix)):\n",
    "            for j in range(1, len(matrix[0])):\n",
    "                if matrix[i][j] == 0:\n",
    "                    matrix[i][0] = 0\n",
    "                    matrix[0][j] = 0\n",
    "\n",
    "        for i in range(1, len(matrix)):\n",
    "            for j in range(1, len(matrix[0])):\n",
    "                if matrix[i][0] == 0 or matrix[0][j] == 0:\n",
    "                    matrix[i][j] = 0\n",
    "\n",
    "        if fst_row:\n",
    "            for j in range(len(matrix[0])):\n",
    "                matrix[0][j] = 0\n",
    "        if fst_col:\n",
    "            for i in range(len(matrix)):\n",
    "                matrix[i][0] = 0\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def setZeroes(self, matrix: List[List[int]]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify matrix in-place instead.\n",
    "        \"\"\"\n",
    "        Row = len(matrix)\n",
    "        Column = len(matrix[0])\n",
    "        row = [0]*Row\n",
    "        column = [0]*Column\n",
    "        for i in range(Row):\n",
    "            for j in range(Column):\n",
    "                if matrix[i][j] == 0:\n",
    "                    row[i] = 1\n",
    "                    column[j] = 1\n",
    "        \n",
    "        for i in range(Row):\n",
    "            for j in range(Column):\n",
    "                if row[i] == 1 or column[j] == 1:\n",
    "                    matrix[i][j] = 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def setZeroes(self, matrix: List[List[int]]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify matrix in-place instead.\n",
    "        \"\"\"\n",
    "        M, N = len(matrix), len(matrix[0])\n",
    "        # 横坐标\n",
    "        row_index = set()\n",
    "        # 纵坐标\n",
    "        col_index = set()\n",
    "\n",
    "        for i in range(M):\n",
    "            for j in range(N):\n",
    "                if matrix[i][j] == 0:\n",
    "                    row_index.add(i)\n",
    "                    col_index.add(j)\n",
    "        \n",
    "        for row in row_index:\n",
    "            matrix[row] = [0] * N\n",
    "        \n",
    "        for col in col_index:\n",
    "            for i in range(M):\n",
    "                matrix[i][col] = 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def setZeroes(self, matrix: List[List[int]]) -> None:\n",
    "        m = len(matrix) # 表示行的长度\n",
    "        n = len(matrix[0]) # 表示列的长度\n",
    "        row = [False] * len(matrix)\n",
    "        column = [False] * len(matrix[0])\n",
    "\n",
    "        # 记录行列为0的元素\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if matrix[i][j] == 0:\n",
    "                    row[i] = True\n",
    "                    column[j] = True\n",
    "        \n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if row[i] or column[j] :\n",
    "                    matrix[i][j] = 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def setZeroes(self, matrix: List[List[int]]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify matrix in-place instead.\n",
    "        \"\"\"\n",
    "        m,n=len(matrix),len(matrix[0])\n",
    "        row,col=[False]*m,[False]*n\n",
    "\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if matrix[i][j] == 0:\n",
    "                    row[i]=col[j]=True\n",
    "        \n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if row[i] or col[j]:\n",
    "                    matrix[i][j]=0\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def setZeroes(self, matrix: List[List[int]]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify matrix in-place instead.\n",
    "        \"\"\"\n",
    "        h = len(matrix)\n",
    "        l = len(matrix[0])\n",
    "        hang = [0]*h\n",
    "        lie = [0]*l\n",
    "        for i in range(h):\n",
    "           for j in range(l):\n",
    "               if matrix[i][j] == 0:\n",
    "                   hang[i] = True\n",
    "                   lie[j] = True\n",
    "        for i in range(h):\n",
    "            for j in range(l):\n",
    "                if hang[i] == True or lie[j] == True:\n",
    "                    matrix[i][j] = 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def setZeroes(self, matrix: List[List[int]]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify matrix in-place instead.\n",
    "        \"\"\"\n",
    "        n=len(matrix)\n",
    "        m=len(matrix[0])\n",
    "        col=set()\n",
    "        row=set()\n",
    "        for i in range(n):\n",
    "            for j in range(m):\n",
    "                if matrix[i][j]==0:\n",
    "                    col.add(j)\n",
    "                    row.add(i)\n",
    "        for i in range(n):\n",
    "            for y in col:\n",
    "                matrix[i][y]=0\n",
    "        for x in row:\n",
    "            matrix[x]=[0]*m\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def setZeroes(self, matrix: List[List[int]]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify matrix in-place instead.\n",
    "        \"\"\"\n",
    "        depth = len(matrix)\n",
    "        width = len(matrix[0])\n",
    "        copy_matrix = [row[:] for row in matrix] \n",
    "        for i in range(depth):\n",
    "            for j in range(width):\n",
    "                if copy_matrix[i][j]==0:\n",
    "                    for k in range(depth):matrix[k][j]=0\n",
    "                    for l in range(width):matrix[i][l]=0\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def setZeroes(self, matrix: List[List[int]]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify matrix in-place instead.\n",
    "        \"\"\"\n",
    "        length_M = len(matrix)\n",
    "        length_N = len(matrix[0])\n",
    "        j_list = []\n",
    "        for i in range(length_M):\n",
    "            status = False\n",
    "            for j in range(length_N):\n",
    "                value = matrix[i][j]\n",
    "                if value == 0:\n",
    "                    status = True\n",
    "                    j_list.append(j)\n",
    "                    for n in range(i):\n",
    "                        matrix[n][j] = 0\n",
    "                elif j in j_list:\n",
    "                    matrix[i][j] = 0\n",
    "            if status:\n",
    "                matrix[i] = [0 for i in range(length_N)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def setZeroes(self, matrix: List[List[int]]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify matrix in-place instead.\n",
    "        \"\"\"\n",
    "        h = len(matrix)\n",
    "        l = len(matrix[0])\n",
    "        hang = [0]*h\n",
    "        lie = [0]*l\n",
    "        for i in range(h):\n",
    "           for j in range(l):\n",
    "               if matrix[i][j] == 0:\n",
    "                   hang[i] = True\n",
    "                   lie[j] = True\n",
    "        for i in range(h):\n",
    "            for j in range(l):\n",
    "                if hang[i] == True or lie[j] == True:\n",
    "                    matrix[i][j] = 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def setZeroes(self, matrix: List[List[int]]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify matrix in-place instead.\n",
    "        \"\"\"\n",
    "        m = len(matrix)\n",
    "        n = len(matrix[0])\n",
    "        row = [0] * m\n",
    "        col = [0] * n\n",
    "\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if matrix[i][j] == 0:\n",
    "                    row[i] = 1\n",
    "                    col[j] = 1\n",
    "\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if row[i] == 1 or col[j] == 1:\n",
    "                    matrix[i][j] = 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def setZeroes(self, matrix: List[List[int]]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify matrix in-place instead.\n",
    "        \"\"\"\n",
    "        m = len(matrix)\n",
    "        n = len(matrix[0])\n",
    "        row = [0] * m\n",
    "        col = [0] * n\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if matrix[i][j] == 0:\n",
    "                    row[i] = 1\n",
    "                    col[j] = 1\n",
    "\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if row[i] == 1 or col[j] == 1:\n",
    "                    matrix[i][j] = 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from typing import List\n",
    "\n",
    "class Solution:\n",
    "    def setZeroes(self, matrix: List[List[int]]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify matrix in-place instead.\n",
    "        \"\"\"\n",
    "        m, n = len(matrix), len(matrix[0])\n",
    "\n",
    "        # 用两个列表记录需要置零的行和列\n",
    "        rows_to_zero = set()\n",
    "        cols_to_zero = set()\n",
    "\n",
    "        for i in range(n):\n",
    "            for j in range(m):\n",
    "                if matrix[j][i] == 0:\n",
    "                    rows_to_zero.add(j)\n",
    "                    cols_to_zero.add(i)\n",
    "\n",
    "        # 置零行\n",
    "        for row in rows_to_zero:\n",
    "            matrix[row] = [0] * n\n",
    "\n",
    "        # 置零列\n",
    "        for col in cols_to_zero:\n",
    "            for k in range(m):\n",
    "                matrix[k][col] = 0\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def setZeroes(self, matrix: List[List[int]]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify matrix in-place instead.\n",
    "        \"\"\"\n",
    "        m = len(matrix)\n",
    "        n = len(matrix[0])\n",
    "        row = [0] * m\n",
    "        column = [0] * n\n",
    "\n",
    "        #如果要整行整列变成0，何必准确定位0的横纵？直接找几行，几列有0。\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if matrix[i][j] == 0:\n",
    "                    row[i] = True\n",
    "                    column[j] = True\n",
    "        \n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if row[i] == True or column[j] == True:\n",
    "                    matrix[i][j] = 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def setZeroes(self, matrix: List[List[int]]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify matrix in-place instead.\n",
    "        \"\"\"\n",
    "        n =len(matrix)#行数\n",
    "        m =len(matrix[0])#列数\n",
    "        need_be_zero = []\n",
    "        for i in range(n):\n",
    "            index_x = i#存储行\n",
    "            for k in range(m):\n",
    "                if matrix[i][k] == 0:\n",
    "                    index_y = k#存储列\n",
    "                    need_be_zero.append([index_x,index_y])\n",
    "        for i in need_be_zero:\n",
    "            for k in range(m):\n",
    "                matrix[i[0]][k] = 0\n",
    "            for z in range(n):\n",
    "                matrix[z][i[1]] = 0\n",
    "        return matrix"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def setZeroes(self, matrix: List[List[int]]) -> None:\n",
    "        rows = [False] * len(matrix)\n",
    "        cols = [False] * len(matrix[0])\n",
    "        for i in range(len(matrix)):\n",
    "            for j in range(len(matrix[i])):\n",
    "                if matrix[i][j] == 0:\n",
    "                    rows[i] = True\n",
    "                    cols[j] = True\n",
    "\n",
    "        for i in range(len(matrix)):\n",
    "            for j in range(len(matrix[i])):\n",
    "                if rows[i] or cols[j]:\n",
    "                    matrix[i][j] = 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def setZeroes(self, matrix: List[List[int]]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify matrix in-place instead.\n",
    "        \"\"\"\n",
    "        # 创建用来记录位置的行列数组\n",
    "        row = [0] * len(matrix)\n",
    "        col = [0] * len(matrix[0])\n",
    "\n",
    "        for i in range(len(matrix)):\n",
    "            for j in range(len(matrix[0])):\n",
    "                if matrix[i][j] == 0:\n",
    "                   # 检测到数字为0，记录位置\n",
    "                   row[i] = 1\n",
    "                   col[j] = 1\n",
    "\n",
    "        for i in range(len(matrix)):\n",
    "            for j in range(len(matrix[0])):\n",
    "                if row[i] == 1 or col[j] == 1:\n",
    "                    matrix[i][j] = 0\n",
    "\n",
    "        return matrix\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def setZeroes(self, matrix: List[List[int]]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify matrix in-place instead.\n",
    "        \"\"\"\n",
    "        m,n = len(matrix),len(matrix[0])\n",
    "        row = [0]*m\n",
    "        column = [0]*n\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if matrix[i][j]==0:\n",
    "                    row[i]=1\n",
    "                    column[j]=1\n",
    "        \n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if row[i]==1 or column[j]==1:\n",
    "                    matrix[i][j]=0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def setZeroes(self, matrix: List[List[int]]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify matrix in-place instead.\n",
    "        \"\"\"\n",
    "        l1 = len(matrix)\n",
    "        l2 = len(matrix[0])\n",
    "        row = [0]*l1\n",
    "        colum = [0]*l2\n",
    "        for i in range(l1):\n",
    "            for j in range(l2):\n",
    "                if matrix[i][j] == 0:\n",
    "                    row[i]=1\n",
    "                    colum[j]=1\n",
    "        \n",
    "        for i in range(l1):\n",
    "            for j in range(l2):\n",
    "                if row[i] == 1 or colum[j]==1:\n",
    "                    matrix[i][j] = 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def setZeroes(self, matrix: List[List[int]]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify matrix in-place instead.\n",
    "        \"\"\"\n",
    "        length_M = len(matrix)\n",
    "        length_N = len(matrix[0])\n",
    "        j_list = []\n",
    "        for i in range(length_M):\n",
    "            status = False\n",
    "            for j in range(length_N):\n",
    "                value = matrix[i][j]\n",
    "                if value == 0:\n",
    "                    status = True\n",
    "                    j_list.append(j)\n",
    "                    for n in range(i):\n",
    "                        matrix[n][j] = 0\n",
    "                elif j in j_list:\n",
    "                    matrix[i][j] = 0\n",
    "            if status:\n",
    "                matrix[i] = [0 for i in range(length_N)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def setZeroes(self, matrix: List[List[int]]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify matrix in-place instead.\n",
    "        \"\"\"\n",
    "        m = len(matrix)\n",
    "        n = len(matrix[0])\n",
    "        row = [0] * m\n",
    "        column = [0] * n\n",
    "\n",
    "        #如果要整行整列变成0，何必准确定位0的横纵？直接找几行，几列有0。\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if matrix[i][j] == 0:\n",
    "                    row[i] = True\n",
    "                    column[j] = True\n",
    "        \n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if row[i] == True or column[j] == True:\n",
    "                    matrix[i][j] = 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def setZeroes(self, matrix: List[List[int]]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify matrix in-place instead.\n",
    "        \"\"\"\n",
    "        i0=set()\n",
    "        j0=set()\n",
    "        for i in range(len(matrix)):\n",
    "            for j in range(len(matrix[0])):\n",
    "                if matrix[i][j]==0:\n",
    "                    i0.add(i)\n",
    "                    j0.add(j)\n",
    "        for i in i0:\n",
    "            for j in range(len(matrix[0])):\n",
    "                matrix[i][j] = 0 \n",
    "        for j in j0:\n",
    "            for i in range(len(matrix)):\n",
    "                matrix[i][j] = 0 \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def setZeroes(self, matrix: List[List[int]]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify matrix in-place instead.\n",
    "        \"\"\"\n",
    "        length_M = len(matrix)\n",
    "        length_N = len(matrix[0])\n",
    "        j_list = []\n",
    "        for i in range(length_M):\n",
    "            status = False\n",
    "            for j in range(length_N):\n",
    "                value = matrix[i][j]\n",
    "                if value == 0:\n",
    "                    status = True\n",
    "                    j_list.append(j)\n",
    "                    for n in range(i):\n",
    "                        matrix[n][j] = 0\n",
    "                elif j in j_list:\n",
    "                    matrix[i][j] = 0\n",
    "            if status:\n",
    "                matrix[i] = [0 for i in range(length_N)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def setZeroes(self, matrix: List[List[int]]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify matrix in-place instead.\n",
    "        \"\"\"\n",
    "        m = len(matrix)\n",
    "        n = len(matrix[0])\n",
    "        zero = []\n",
    "        print (m, n)\n",
    "\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if matrix[i][j] == 0:\n",
    "                    zero.append([i,j])\n",
    "        \n",
    "        print (zero)\n",
    "\n",
    "        row = set()\n",
    "        col = set()\n",
    "\n",
    "        for i in range(len(zero)):\n",
    "            if zero[i][0] not in row:\n",
    "                row.add(zero[i][0]) \n",
    "            if zero[i][1] not in col:\n",
    "                col.add(zero[i][1]) \n",
    "\n",
    "        for i in row:\n",
    "            for j in range(n):          \n",
    "                matrix[i][j] = 0\n",
    "        \n",
    "        for j in col:\n",
    "            for i in range(m):          \n",
    "                matrix[i][j] = 0\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def setZeroes(self, matrix: List[List[int]]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify matrix in-place instead.\n",
    "        \"\"\"\n",
    "        m = len(matrix)\n",
    "        n = len(matrix[0])\n",
    "        row = [0] * m\n",
    "        column = [0] * n\n",
    "\n",
    "        #如果要整行整列变成0，何必准确定位0的横纵？直接找几行，几列有0。\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if matrix[i][j] == 0:\n",
    "                    row[i] = True\n",
    "                    column[j] = True\n",
    "        \n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if row[i] == True or column[j] == True:\n",
    "                    matrix[i][j] = 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def setZeroes(self, matrix: List[List[int]]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify matrix in-place instead.\n",
    "        \"\"\"\n",
    "        res = [0]*len(matrix)\n",
    "        red = [0]*len(matrix[0])\n",
    "        n = len(matrix[0])\n",
    "        m = len(matrix)\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if matrix[i][j] == 0:\n",
    "                    res[i] = 1\n",
    "                    red[j]= 1\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if res[i] == 1 or red[j]==1:\n",
    "                    matrix[i][j]=0\n",
    "\n",
    "\n",
    "\n",
    "        \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def setZeroes(self, matrix: List[List[int]]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify matrix in-place instead.\n",
    "        \"\"\"\n",
    "        rows = set()\n",
    "        lines = set()\n",
    "        lenr = len(matrix)\n",
    "        lenl = len(matrix[0])\n",
    "        for i in range(lenr):\n",
    "            for j in range(lenl):\n",
    "                if matrix[i][j] == 0:\n",
    "                    if i not in rows:\n",
    "                        rows.add(i)\n",
    "                    if j not in lines:\n",
    "                        lines.add(j)\n",
    "        for i in range(lenr):\n",
    "            if i in rows:\n",
    "                matrix[i] = [0] * lenl\n",
    "            else:\n",
    "                for line in lines:\n",
    "                    matrix[i][line] = 0 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "\n",
    "    def _set_zero(self, row, col, matrix):\n",
    "        row_len=len(matrix)\n",
    "        col_len=len(matrix[0])\n",
    "\n",
    "        for r in range(row_len):\n",
    "            matrix[r][col]=0\n",
    "        for c in range(col_len):\n",
    "            matrix[row][c]=0\n",
    "\n",
    "    def setZeroes(self, matrix: List[List[int]]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify matrix in-place instead.\n",
    "        \"\"\"\n",
    "        zero=[]\n",
    "        for i in range(len(matrix)):\n",
    "            for j in range(len(matrix[0])):\n",
    "                if matrix[i][j]==0:\n",
    "                    zero.append([i,j])\n",
    "        for z in zero:\n",
    "            self._set_zero(z[0],z[1],matrix)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def setZeroes(self, matrix: List[List[int]]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify matrix in-place instead.\n",
    "        \"\"\"\n",
    "        res = [0]*len(matrix)\n",
    "        red = [0]*len(matrix[0])\n",
    "        n = len(matrix[0])\n",
    "        m = len(matrix)\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if matrix[i][j] == 0:\n",
    "                    res[i] = 1\n",
    "                    red[j]= 1\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if res[i] == 1 or red[j]==1:\n",
    "                    matrix[i][j]=0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def setZeroes(self, matrix: List[List[int]]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify matrix in-place instead.\n",
    "        \"\"\"\n",
    "        n = len(matrix[0])\n",
    "        m = len(matrix)\n",
    "        memo_col = []\n",
    "        memo_row = []\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if matrix[i][j] == 0:\n",
    "                    memo_row.append(i)\n",
    "                    break\n",
    "        for i in range(n):\n",
    "            for j in range(m):\n",
    "                if matrix[j][i] == 0:\n",
    "                    memo_col.append(i)\n",
    "                    break\n",
    "        for i in range(len(memo_row)):\n",
    "            matrix[memo_row[i]] = [0] * n\n",
    "        for i in range(len(memo_col)):\n",
    "            for j in range(m):\n",
    "                matrix[j][memo_col[i]] = 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def setZeroes(self, matrix: List[List[int]]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify matrix in-place instead.\n",
    "        \"\"\"\n",
    "        hang = len(matrix)\n",
    "        lie = len(matrix[0])\n",
    "        arr = [[x for x in range(1,lie+1)]for y in range(hang)]\n",
    "        for i in range(hang):\n",
    "            for j in range(lie):\n",
    "                if matrix[i][j] == 0:\n",
    "                    arr[i][j] = 0\n",
    "        for i in range(hang):\n",
    "            for j in range(lie):\n",
    "                if arr[i][j] == 0:\n",
    "                    for k in range(hang):\n",
    "                        matrix[k][j] = 0\n",
    "                    for k in range(lie):\n",
    "                        matrix[i][k] = 0 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def setZeroes(self, matrix: List[List[int]]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify matrix in-place instead.\n",
    "        \"\"\"\n",
    "        row = []\n",
    "        column = []\n",
    "        for i in range(len(matrix)):\n",
    "            for j in range(len(matrix[0])):\n",
    "                if matrix[i][j] == 0:\n",
    "                    row.append(i)\n",
    "                    column.append(j)\n",
    "        row = set(row)\n",
    "        column = set(column)\n",
    "        for i in range(len(matrix)):\n",
    "            for j in range(len(matrix[0])):\n",
    "                if i in row or j in column:\n",
    "                    matrix[i][j] = 0\n",
    "        return matrix"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def setZeroes(self, matrix: List[List[int]]) -> None:\n",
    "        x = {}\n",
    "        y = {}\n",
    "        for row in range(len(matrix)):\n",
    "            for col in range(len(matrix[0])):\n",
    "                if matrix[row][col] == 0:\n",
    "                    x[row] = True\n",
    "                    y[col] = True\n",
    "        for row in range(len(matrix)):\n",
    "            for col in range(len(matrix[0])):\n",
    "                if x.get(row) or y.get(col):\n",
    "                    matrix[row][col] = 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def setZeroes(self, matrix: List[List[int]]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify matrix in-place instead.\n",
    "        \"\"\"\n",
    "        tmp = []\n",
    "        m, n = len(matrix), len(matrix[0])\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if matrix[i][j] == 0:\n",
    "                    tmp.append([i, j])\n",
    "        while tmp:\n",
    "            i, j = tmp.pop()\n",
    "            for k in range(n):\n",
    "                matrix[i][k] = 0\n",
    "            for k in range(m):\n",
    "                matrix[k][j] = 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def setZeroes(self, matrix: List[List[int]]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify matrix in-place instead.\n",
    "        \"\"\"\n",
    "        m=len(matrix)\n",
    "        n=len(matrix[0])\n",
    "        t=[[],[]]\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if matrix[i][j]==0:\n",
    "                    if i not in t[0]:\n",
    "                        t[0].append(i)\n",
    "                    if j not in t[1]:\n",
    "                        t[1].append(j)\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if i in t[0] or j in t[1]:\n",
    "                    matrix[i][j]=0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def setZeroes(self, matrix: List[List[int]]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify matrix in-place instead.\n",
    "        \"\"\"\n",
    "        zero_positions = [(i,j) for i,row in enumerate(matrix) for j, value in enumerate(row) if value == 0]\n",
    "        for i,j in zero_positions:\n",
    "            matrix[i] = [0]*len(matrix[0])\n",
    "            for k in range(len(matrix)):\n",
    "                matrix[k][j] = 0\n",
    "        return matrix \n",
    "\n",
    "\n",
    "       "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def setZeroes(self, matrix: List[List[int]]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify matrix in-place instead.\n",
    "        \"\"\"\n",
    "        # count zero idx\n",
    "        m,n=len(matrix),len(matrix[0])\n",
    "        seen=[[0 for _ in range(n)] for _ in range(m)]\n",
    "        def reset(row,col):\n",
    "            for j in range(n):\n",
    "                if matrix[row][j]!=0:\n",
    "                    matrix[row][j]=0\n",
    "                    seen[row][j]=1\n",
    "            for j in range(m):\n",
    "                if matrix[j][col]!=0:\n",
    "                    matrix[j][col]=0\n",
    "                    seen[j][col]=1\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if matrix[i][j]==0 and not seen[i][j]:\n",
    "                    reset(i,j)\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def setZeroes(self, matrix: List[List[int]]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify matrix in-place instead.\n",
    "        \"\"\"\n",
    "        row=len(matrix)\n",
    "        col=len(matrix[0])\n",
    "        list_row=[]\n",
    "        list_col=[]\n",
    "\n",
    "        #查找数组中为0的数据\n",
    "        for i in range(row):\n",
    "            for j in range(col):\n",
    "                if matrix[i][j]==0:\n",
    "                    list_row.append(i)\n",
    "                    list_col.append(j)\n",
    "\n",
    "        #更新数组\n",
    "        for i in range(row):\n",
    "            if i in list_row:\n",
    "                for j in range(col):\n",
    "                    matrix[i][j]=0\n",
    "\n",
    "        for j in range(col):\n",
    "            if j in list_col:\n",
    "                for i in range(row):\n",
    "                    matrix[i][j]=0\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def setZeroes(self, matrix: List[List[int]]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify matrix in-place instead.\n",
    "        \"\"\"\n",
    "        n=len(matrix)\n",
    "        m=len(matrix[0])\n",
    "        xt=set()\n",
    "        yt=set()\n",
    "        for i in range(n):\n",
    "            for j in range(m):\n",
    "                if matrix[i][j]==0:\n",
    "                    xt.add(i)\n",
    "                    yt.add(j)\n",
    "        print(xt)\n",
    "        print(yt)\n",
    "        for i in range(n):\n",
    "            for j in range(m):\n",
    "                if i in xt or j in yt:\n",
    "                    matrix[i][j]=0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def setZeroes(self, matrix: List[List[int]]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify matrix class Solution:\n",
    "\n",
    "        \n",
    "\n",
    "        Do not return anything, modify matrix in-place instead.\n",
    "\n",
    "        \"\"\"\n",
    "\n",
    "        m = len(matrix)\n",
    "\n",
    "        n = len(matrix[0])\n",
    "\n",
    "        row = [0] * len(matrix)\n",
    "\n",
    "        column = [0] *len(matrix[0])\n",
    "\n",
    "        # 统计、记录行列为0的元素\n",
    "\n",
    "        for i in range(m):\n",
    "\n",
    "            for j in range(n):\n",
    "\n",
    "                if matrix[i][j] == 0:\n",
    "\n",
    "                    row[i] = 1\n",
    "\n",
    "                    column[j] = 1\n",
    "\n",
    "        \n",
    "\n",
    "        for i in range(m):\n",
    "\n",
    "            for j in range(n):\n",
    "\n",
    "                if row[i] == 1 or column[j] == 1:\n",
    "\n",
    "                    matrix[i][j] = 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def setZeroes(self, matrix: List[List[int]]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify matrix in-place instead.\n",
    "        \"\"\"\n",
    "        if matrix == []:\n",
    "            return matrix\n",
    "        row = len(matrix)\n",
    "        col = len(matrix[0])\n",
    "        row_0 = []\n",
    "        col_0 = []\n",
    "        for i in range(row):\n",
    "            for j in range(col):\n",
    "                if matrix[i][j] == 0:\n",
    "                    row_0.append(i)\n",
    "                    col_0.append(j)\n",
    "\n",
    "        for i in row_0:\n",
    "            for j in range(col):\n",
    "                matrix[i][j] = 0\n",
    "\n",
    "        for j in col_0:\n",
    "            for i in range(row):\n",
    "                matrix[i][j] = 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def setZeroes(self, matrix: List[List[int]]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify matrix in-place instead.\n",
    "        \"\"\"\n",
    "        row = len(matrix)  # 行\n",
    "        col = len(matrix[0])  # 列\n",
    "        alphabet = []\n",
    "        for i in range(row):\n",
    "            for j in range(col):\n",
    "                if matrix[i][j] == 0:\n",
    "                    alphabet.append((i, j))\n",
    "\n",
    "        for k, v in alphabet:\n",
    "            for j in range(col):\n",
    "                matrix[k][j] = 0\n",
    "            for i in range(row):\n",
    "                matrix[i][v] = 0\n",
    "\n",
    "        return matrix\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def setZeroes(self, matrix: List[List[int]]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify matrix in-place instead.\n",
    "        \"\"\"\n",
    "        length_M = len(matrix)\n",
    "        length_N = len(matrix[0])\n",
    "        j_list = []\n",
    "        for i in range(length_M):\n",
    "            status = False\n",
    "            for j in range(length_N):\n",
    "                value = matrix[length_M - i - 1][length_N - j - 1]\n",
    "                if value == 0:\n",
    "                    status = True\n",
    "                    j_list.append(length_N-j-1)\n",
    "                    print(j_list)\n",
    "                    for n in range(length_M-i, length_M):\n",
    "                        matrix[n][length_N - j - 1] = 0\n",
    "                elif length_N - j - 1 in j_list:\n",
    "                    matrix[length_M - i - 1][length_N - j - 1] = 0\n",
    "            if status:\n",
    "                matrix[length_M - i - 1] = [0 for i in range(length_N)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def setZeroes(self, matrix: List[List[int]]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify matrix in-place instead.\n",
    "        \"\"\"\n",
    "        tmp = copy.deepcopy(matrix)\n",
    "        row = len(matrix)\n",
    "        column = len(matrix[0])\n",
    "        for i in range(row) :\n",
    "            for j in range(column) :\n",
    "                if tmp[i][j] ==0 :\n",
    "                    for k in range(row) :\n",
    "                        matrix[k][j] = 0\n",
    "                    for l in range(column) :\n",
    "                        matrix[i][l] = 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def setZeroes(self, matrix: List[List[int]]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify matrix in-place instead.\n",
    "        \"\"\"\n",
    "        i1 = []\n",
    "        j1 = []\n",
    "        m = len(matrix)\n",
    "        n = len(matrix[0])\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if matrix[i][j] == 0:\n",
    "                    i1.append(i)\n",
    "                    j1.append(j)\n",
    "                    print(i1,j1)\n",
    "        # i1 = set(i1)\n",
    "        # j1 = set(j1)\n",
    "        print(i1,j1)\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if (i in i1) or (j in j1):\n",
    "                    matrix[i][j] = 0\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def setZeroes(self, matrix: List[List[int]]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify matrix in-place instead.\n",
    "        \"\"\"\n",
    "        hang = len(matrix)\n",
    "        lie = len(matrix[0])\n",
    "        arr = [[x for x in range(1,lie+1)]for y in range(hang)]\n",
    "        for i in range(hang):\n",
    "            for j in range(lie):\n",
    "                if matrix[i][j] == 0:\n",
    "                    arr[i][j] = 0\n",
    "        for i in range(hang):\n",
    "            for j in range(lie):\n",
    "                if arr[i][j] == 0:\n",
    "                    for k in range(hang):\n",
    "                        matrix[k][j] = 0\n",
    "                    for k in range(lie):\n",
    "                        matrix[i][k] = 0 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def setZeroes(self, matrix: List[List[int]]) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify matrix in-place instead.\n",
    "        \"\"\"\n",
    "        M = len(matrix)\n",
    "        N = len(matrix[0])\n",
    "        zero_matrix = [[1] * N for _ in range(M)]  # 创建一个与原始矩阵相同大小的零矩阵\n",
    "\n",
    "        for i in range(M):\n",
    "            for j in range(N):\n",
    "                if matrix[i][j] == 0:\n",
    "                    # 在零矩阵中标记对应的行和列\n",
    "                    for k in range(M):\n",
    "                        zero_matrix[k][j] = 0\n",
    "                    for k in range(N):\n",
    "                        zero_matrix[i][k] = 0\n",
    "\n",
    "        # 将标记好的零矩阵中的值赋给原始矩阵\n",
    "        for i in range(M):\n",
    "            for j in range(N):\n",
    "                if zero_matrix[i][j] != 1:\n",
    "                     matrix[i][j] = zero_matrix[i][j]"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
